<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>ansible 常用模块 | 小鹏杂谈</title>
    <meta name="generator" content="VuePress 1.8.2">
    <link rel="icon" href="/favicon.ico">
    <meta name="description" content="小鹏杂谈">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="/assets/css/0.styles.8516fda1.css" as="style"><link rel="preload" href="/assets/js/app.b9ccfc55.js" as="script"><link rel="preload" href="/assets/js/4.8390fbcb.js" as="script"><link rel="preload" href="/assets/js/1.3334f995.js" as="script"><link rel="preload" href="/assets/js/62.a1a460c7.js" as="script"><link rel="prefetch" href="/assets/js/10.8eb4e356.js"><link rel="prefetch" href="/assets/js/11.31befa4c.js"><link rel="prefetch" href="/assets/js/12.da37eb75.js"><link rel="prefetch" href="/assets/js/13.b24ba73c.js"><link rel="prefetch" href="/assets/js/14.3bbed283.js"><link rel="prefetch" href="/assets/js/15.fac60f8d.js"><link rel="prefetch" href="/assets/js/16.b75d2036.js"><link rel="prefetch" href="/assets/js/17.116317c5.js"><link rel="prefetch" href="/assets/js/18.80fdbeda.js"><link rel="prefetch" href="/assets/js/19.459fc46f.js"><link rel="prefetch" href="/assets/js/20.452f30f9.js"><link rel="prefetch" href="/assets/js/21.99f641dc.js"><link rel="prefetch" href="/assets/js/22.081dc33b.js"><link rel="prefetch" href="/assets/js/23.d3455a59.js"><link rel="prefetch" href="/assets/js/24.8f5d39e0.js"><link rel="prefetch" href="/assets/js/25.c3dc47fb.js"><link rel="prefetch" href="/assets/js/26.d93b5cf2.js"><link rel="prefetch" href="/assets/js/27.6d2089a5.js"><link rel="prefetch" href="/assets/js/28.57336847.js"><link rel="prefetch" href="/assets/js/29.903b5e06.js"><link rel="prefetch" href="/assets/js/3.32100170.js"><link rel="prefetch" href="/assets/js/30.274666a3.js"><link rel="prefetch" href="/assets/js/31.fa9bd0f8.js"><link rel="prefetch" href="/assets/js/32.20179720.js"><link rel="prefetch" href="/assets/js/33.2dcc611d.js"><link rel="prefetch" href="/assets/js/34.ba79122c.js"><link rel="prefetch" href="/assets/js/35.bcd9298f.js"><link rel="prefetch" href="/assets/js/36.bf6245d9.js"><link rel="prefetch" href="/assets/js/37.734eb893.js"><link rel="prefetch" href="/assets/js/38.26999070.js"><link rel="prefetch" href="/assets/js/39.cc1f016d.js"><link rel="prefetch" href="/assets/js/40.da84c263.js"><link rel="prefetch" href="/assets/js/41.a96b8efa.js"><link rel="prefetch" href="/assets/js/42.d586cccb.js"><link rel="prefetch" href="/assets/js/43.69bb61aa.js"><link rel="prefetch" href="/assets/js/44.3be5a840.js"><link rel="prefetch" href="/assets/js/45.208a3bc3.js"><link rel="prefetch" href="/assets/js/46.2d6cacad.js"><link rel="prefetch" href="/assets/js/47.5add378a.js"><link rel="prefetch" href="/assets/js/48.587139cc.js"><link rel="prefetch" href="/assets/js/49.57f7c25a.js"><link rel="prefetch" href="/assets/js/5.ff1e84d1.js"><link rel="prefetch" href="/assets/js/50.efde6e4a.js"><link rel="prefetch" href="/assets/js/51.c9447e5f.js"><link rel="prefetch" href="/assets/js/52.57a51c35.js"><link rel="prefetch" href="/assets/js/53.4be0f240.js"><link rel="prefetch" href="/assets/js/54.8dabd349.js"><link rel="prefetch" href="/assets/js/55.3b8ae0e4.js"><link rel="prefetch" href="/assets/js/56.07e830b2.js"><link rel="prefetch" href="/assets/js/57.8c0dbdcc.js"><link rel="prefetch" href="/assets/js/58.63387df6.js"><link rel="prefetch" href="/assets/js/59.959bb8c8.js"><link rel="prefetch" href="/assets/js/6.56a2b5cd.js"><link rel="prefetch" href="/assets/js/60.1041d86f.js"><link rel="prefetch" href="/assets/js/61.f58e6c18.js"><link rel="prefetch" href="/assets/js/63.6c611c7d.js"><link rel="prefetch" href="/assets/js/64.6711119c.js"><link rel="prefetch" href="/assets/js/65.4655c3b1.js"><link rel="prefetch" href="/assets/js/66.e5dfe864.js"><link rel="prefetch" href="/assets/js/67.37515de0.js"><link rel="prefetch" href="/assets/js/68.0c8ba92d.js"><link rel="prefetch" href="/assets/js/69.52121d38.js"><link rel="prefetch" href="/assets/js/7.6f143d9f.js"><link rel="prefetch" href="/assets/js/70.1ab4d6a1.js"><link rel="prefetch" href="/assets/js/71.dc99dd1d.js"><link rel="prefetch" href="/assets/js/72.02e21e52.js"><link rel="prefetch" href="/assets/js/73.21efc121.js"><link rel="prefetch" href="/assets/js/74.649f4e1d.js"><link rel="prefetch" href="/assets/js/75.331c13e7.js"><link rel="prefetch" href="/assets/js/76.a94b5ff0.js"><link rel="prefetch" href="/assets/js/8.d4d40f2a.js"><link rel="prefetch" href="/assets/js/9.1b8824e7.js">
    <link rel="stylesheet" href="/assets/css/0.styles.8516fda1.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container no-sidebar" data-v-1156296a><div data-v-1156296a><div id="loader-wrapper" class="loading-wrapper" data-v-d48f4d20 data-v-1156296a data-v-1156296a><div class="loader-main" data-v-d48f4d20><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div></div> <!----> <!----></div> <div class="password-shadow password-wrapper-out" style="display:none;" data-v-4e82dffc data-v-1156296a data-v-1156296a><h3 class="title" data-v-4e82dffc data-v-4e82dffc>小鹏杂谈</h3> <p class="description" data-v-4e82dffc data-v-4e82dffc>小鹏杂谈</p> <label id="box" class="inputBox" data-v-4e82dffc data-v-4e82dffc><input type="password" value="" data-v-4e82dffc> <span data-v-4e82dffc>Konck! Knock!</span> <button data-v-4e82dffc>OK</button></label> <div class="footer" data-v-4e82dffc data-v-4e82dffc><span data-v-4e82dffc><i class="iconfont reco-theme" data-v-4e82dffc></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-4e82dffc>vuePress-theme-reco</a></span> <span data-v-4e82dffc><i class="iconfont reco-copyright" data-v-4e82dffc></i> <a data-v-4e82dffc><span data-v-4e82dffc>lzpeng723</span>
            
          <span data-v-4e82dffc>2021 - </span>
          2022
        </a></span></div></div> <div class="hide" data-v-1156296a><header class="navbar" data-v-1156296a><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/logo.png" alt="小鹏杂谈" class="logo"> <span class="site-name">小鹏杂谈</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/运维/" class="nav-link"><i class="undefined"></i>
  运维
</a></li><li class="dropdown-item"><!----> <a href="/categories/开发/" class="nav-link"><i class="undefined"></i>
  开发
</a></li><li class="dropdown-item"><!----> <a href="/categories/后端/" class="nav-link"><i class="undefined"></i>
  后端
</a></li><li class="dropdown-item"><!----> <a href="/categories/前端/" class="nav-link"><i class="undefined"></i>
  前端
</a></li><li class="dropdown-item"><!----> <a href="/categories/软件/" class="nav-link"><i class="undefined"></i>
  软件
</a></li><li class="dropdown-item"><!----> <a href="/categories/文档/" class="nav-link"><i class="undefined"></i>
  文档
</a></li><li class="dropdown-item"><!----> <a href="/categories/数据库/" class="nav-link"><i class="undefined"></i>
  数据库
</a></li><li class="dropdown-item"><!----> <a href="/categories/Node.js/" class="nav-link"><i class="undefined"></i>
  Node.js
</a></li><li class="dropdown-item"><!----> <a href="/categories/游戏/" class="nav-link"><i class="undefined"></i>
  游戏
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间线
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      文档
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/docs/minimal-boot/" class="nav-link"><i class="undefined"></i>
  minimal-boot
</a></li><li class="dropdown-item"><!----> <a href="/docs/minimal-cloud/" class="nav-link"><i class="undefined"></i>
  minimal-cloud
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      联系方式
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://github.com/lzpeng723" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://gitee.com/lzpeng723" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-mayun"></i>
  Gitee
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://www.zhihu.com/people/lzpeng723" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-zhihu"></i>
  知乎
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-1156296a></div> <aside class="sidebar" data-v-1156296a><div class="personal-info-wrapper" data-v-828910c6 data-v-1156296a><img src="/avatar.jpg" alt="author-avatar" class="personal-img" data-v-828910c6> <h3 class="name" data-v-828910c6>
    lzpeng723
  </h3> <div class="num" data-v-828910c6><div data-v-828910c6><h3 data-v-828910c6>66</h3> <h6 data-v-828910c6>Articles</h6></div> <div data-v-828910c6><h3 data-v-828910c6>19</h3> <h6 data-v-828910c6>Tags</h6></div></div> <ul class="social-links" data-v-828910c6></ul> <hr data-v-828910c6></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/运维/" class="nav-link"><i class="undefined"></i>
  运维
</a></li><li class="dropdown-item"><!----> <a href="/categories/开发/" class="nav-link"><i class="undefined"></i>
  开发
</a></li><li class="dropdown-item"><!----> <a href="/categories/后端/" class="nav-link"><i class="undefined"></i>
  后端
</a></li><li class="dropdown-item"><!----> <a href="/categories/前端/" class="nav-link"><i class="undefined"></i>
  前端
</a></li><li class="dropdown-item"><!----> <a href="/categories/软件/" class="nav-link"><i class="undefined"></i>
  软件
</a></li><li class="dropdown-item"><!----> <a href="/categories/文档/" class="nav-link"><i class="undefined"></i>
  文档
</a></li><li class="dropdown-item"><!----> <a href="/categories/数据库/" class="nav-link"><i class="undefined"></i>
  数据库
</a></li><li class="dropdown-item"><!----> <a href="/categories/Node.js/" class="nav-link"><i class="undefined"></i>
  Node.js
</a></li><li class="dropdown-item"><!----> <a href="/categories/游戏/" class="nav-link"><i class="undefined"></i>
  游戏
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间线
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      文档
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/docs/minimal-boot/" class="nav-link"><i class="undefined"></i>
  minimal-boot
</a></li><li class="dropdown-item"><!----> <a href="/docs/minimal-cloud/" class="nav-link"><i class="undefined"></i>
  minimal-cloud
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      联系方式
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://github.com/lzpeng723" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://gitee.com/lzpeng723" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-mayun"></i>
  Gitee
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://www.zhihu.com/people/lzpeng723" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-zhihu"></i>
  知乎
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <!----></nav> <!----> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-4e82dffc data-v-1156296a><h3 class="title" data-v-4e82dffc data-v-4e82dffc>ansible 常用模块</h3> <!----> <label id="box" class="inputBox" data-v-4e82dffc data-v-4e82dffc><input type="password" value="" data-v-4e82dffc> <span data-v-4e82dffc>Konck! Knock!</span> <button data-v-4e82dffc>OK</button></label> <div class="footer" data-v-4e82dffc data-v-4e82dffc><span data-v-4e82dffc><i class="iconfont reco-theme" data-v-4e82dffc></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-4e82dffc>vuePress-theme-reco</a></span> <span data-v-4e82dffc><i class="iconfont reco-copyright" data-v-4e82dffc></i> <a data-v-4e82dffc><span data-v-4e82dffc>lzpeng723</span>
            
          <span data-v-4e82dffc>2021 - </span>
          2022
        </a></span></div></div> <div data-v-1156296a><main class="page"><section><div class="page-title"><h1 class="title">ansible 常用模块</h1> <div data-v-1ff7123e><i class="iconfont reco-account" data-v-1ff7123e><span data-v-1ff7123e>lzpeng723</span></i> <i class="iconfont reco-date" data-v-1ff7123e><span data-v-1ff7123e>10/17/2021</span></i> <!----> <i class="tags iconfont reco-tag" data-v-1ff7123e><span class="tag-item" data-v-1ff7123e>ansible</span></i></div></div> <div class="theme-reco-content content__default"><h2 id="ansible-常用模块"><a href="#ansible-常用模块" class="header-anchor">#</a> ansible 常用模块</h2> <h3 id="_1-主机连通性测试"><a href="#_1-主机连通性测试" class="header-anchor">#</a> 1）主机连通性测试</h3> <p>我们使用<code>ansible test -m ping</code>命令来进行主机连通性测试，效果如下：</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>ansible <span class="token builtin class-name">test</span> -m <span class="token function">ping</span>

CentOS7-Node1 <span class="token operator">|</span> SUCCESS <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
    <span class="token string">&quot;ansible_facts&quot;</span><span class="token builtin class-name">:</span> <span class="token punctuation">{</span>
        <span class="token string">&quot;discovered_interpreter_python&quot;</span><span class="token builtin class-name">:</span> <span class="token string">&quot;/usr/bin/python&quot;</span>
    <span class="token punctuation">}</span>, 
    <span class="token string">&quot;changed&quot;</span><span class="token builtin class-name">:</span> false, 
    <span class="token string">&quot;ping&quot;</span><span class="token builtin class-name">:</span> <span class="token string">&quot;pong&quot;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>这样就说明我们的主机是连通状态的。接下来的操作才可以正常进行。</p> <h3 id="_2-command-模块"><a href="#_2-command-模块" class="header-anchor">#</a> 2）command 模块</h3> <p>command 模块为默认模块，所以<code>-m command</code>可以不写
这个模块可以直接在远程主机上执行命令，并将结果返回本主机。
举例如下：</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>ansible <span class="token builtin class-name">test</span> -m <span class="token builtin class-name">command</span> -a <span class="token string">'uptime'</span>

CentOS7-Node1 <span class="token operator">|</span> CHANGED <span class="token operator">|</span> <span class="token assign-left variable">rc</span><span class="token operator">=</span><span class="token number">0</span> <span class="token operator">&gt;&gt;</span>
 <span class="token number">21</span>:40:02 up  <span class="token number">3</span>:33,  <span class="token number">1</span> user,  load average: <span class="token number">0.01</span>, <span class="token number">0.02</span>, <span class="token number">0.05</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>命令模块接受命令名称，后面是空格分隔的列表参数。给定的命令将在所有选定的节点上执行。它不会通过shell进行处理，比如$HOME和操作如&quot;&lt;&quot;，&quot;&gt;&quot;，&quot;|&quot;，&quot;;&quot;，&quot;&amp;&quot; 工作（需要使用（shell）模块实现这些功能）。注意，该命令不支持<code>|</code> 管道命令。</p> <p>下面来看一看该模块下常用的几个命令：</p> <blockquote><p>chdir # 在执行命令之前，先切换到该目录
executable # 切换shell来执行命令，需要使用命令的绝对路径
free_form # 要执行的Linux指令，一般使用Ansible的-a参数代替。
creates # 一个文件名，当这个文件存在，则该命令不执行
removes # 一个文件名，当这个文件不存在，则该命令不执行</p></blockquote> <p>下面我们来看看这些命令的执行效果：</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token comment"># 先切换到 /tmp/ 目录，再执行 touch a.txt b.txt c.txt 命令</span>
ansible <span class="token builtin class-name">test</span> -m <span class="token builtin class-name">command</span> -a <span class="token string">'chdir=/tmp/ touch a.txt b.txt c.txt'</span>

CentOS7-Node1 <span class="token operator">|</span> CHANGED <span class="token operator">|</span> <span class="token assign-left variable">rc</span><span class="token operator">=</span><span class="token number">0</span> <span class="token operator">&gt;&gt;</span>

<span class="token comment">#如果 /tmp/a.txt 存在，则不执行 touch d.txt 命令</span>
ansible <span class="token builtin class-name">test</span> -m <span class="token builtin class-name">command</span> -a <span class="token string">'chdir=/tmp/ creates=/tmp/a.txt touch d.txt'</span>

CentOS7-Node1 <span class="token operator">|</span> SUCCESS <span class="token operator">|</span> <span class="token assign-left variable">rc</span><span class="token operator">=</span><span class="token number">0</span> <span class="token operator">&gt;&gt;</span>
skipped, since /tmp/a.txt exists

<span class="token comment">#如果 /tmp/a.txt 存在，则执行 rm -rf a.txt b.txt c.txt d.txt 命令</span>
ansible <span class="token builtin class-name">test</span> -m <span class="token builtin class-name">command</span> -a <span class="token string">'chdir=/tmp/ removes=/tmp/a.txt rm -rf a.txt b.txt c.txt d.txt'</span>

CentOS7-Node1 <span class="token operator">|</span> CHANGED <span class="token operator">|</span> <span class="token assign-left variable">rc</span><span class="token operator">=</span><span class="token number">0</span> <span class="token operator">&gt;&gt;</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><h3 id="_3-shell-模块"><a href="#_3-shell-模块" class="header-anchor">#</a> 3）shell 模块</h3> <p>shell模块可以在远程主机上调用shell解释器运行命令，支持shell的各种功能，例如管道等。</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token comment">#进程数量</span>
ansible <span class="token builtin class-name">test</span> -m shell -a <span class="token string">'ps -aux | wc -l'</span>
<span class="token comment"># 如果密钥不存在则生成密钥</span>
ansible <span class="token builtin class-name">test</span> -m shell -a <span class="token string">&quot;ssh-keygen -f ~/.ssh/id_rsa -N '' creates=~/.ssh/id_rsa&quot;</span>
<span class="token comment"># 当存在 unzip 命令时才执行解压操作</span>
ansible <span class="token builtin class-name">test</span> -m shell -a <span class="token string">&quot;upzip xx.zip removes=/bin/unzip&quot;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>只要是我们的shell命令，都可以通过这个模块在远程主机上运行，这里就不一一举例了。</p> <blockquote><p>注: 使用<code>command</code>模块和<code>shell</code>模块必须执行非交互式命令，不可执行交互式命令，比如<code>vim</code>,<code>top</code>等。</p></blockquote> <h3 id="_4-copy-模块"><a href="#_4-copy-模块" class="header-anchor">#</a> 4）copy 模块</h3> <p>这个模块用于将文件复制到远程主机，同时支持给定内容生成文件和修改权限等。
　　其相关选项如下：</p> <blockquote><p><code>src</code> # 被复制到远程主机的本地文件。可以是绝对路径，也可以是相对路径。如果路径是一个目录，则会递归复制，用法类似于&quot;rsync&quot;
<code>content</code> # 用于替换&quot;src&quot;，可以直接指定文件的值
<code>dest</code> # 必选项，将源文件复制到的远程主机的<strong>绝对路径</strong> <code>backup</code> # 当文件内容发生改变后，在覆盖之前把源文件备份，备份文件包含时间信息
<code>directory_mode</code> # 递归设定目录的权限，默认为系统默认权限
<code>force</code> # 当目标主机包含该文件，但内容不同时，设为&quot;yes&quot;，表示强制覆盖；设为&quot;no&quot;，表示目标主机的目标位置不存在该文件才复制。默认为&quot;yes&quot;
<code>others</code> # 所有的 file 模块中的选项可以在这里使用</p></blockquote> <p>用法举例如下：</p> <h4 id="复制文件"><a href="#复制文件" class="header-anchor">#</a> 复制文件：</h4> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token builtin class-name">echo</span> AAA <span class="token operator">&gt;&gt;</span> ./temp.txt
ansible <span class="token builtin class-name">test</span> -m copy -a <span class="token string">'src=./temp.txt dest=/tmp/temp123.txt mode=666'</span>
ansible <span class="token builtin class-name">test</span> -a <span class="token string">'ls -l /tmp/temp123.txt'</span>

<span class="token function">rm</span> -f ./temp.txt
ansible <span class="token builtin class-name">test</span> -m <span class="token function">file</span> -a <span class="token string">'path=/tmp/temp123.txt state=absent'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><h4 id="给定内容生成文件-并制定权限"><a href="#给定内容生成文件-并制定权限" class="header-anchor">#</a> 给定内容生成文件，并制定权限</h4> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>ansible <span class="token builtin class-name">test</span> -m copy -a <span class="token string">'content=&quot;I am Lzpeng&quot; dest=/tmp/temp123.txt mode=666'</span>
ansible <span class="token builtin class-name">test</span> -a <span class="token string">'ls -l /tmp/temp123.txt'</span>

ansible <span class="token builtin class-name">test</span> -m <span class="token function">file</span> -a <span class="token string">'path=/tmp/temp123.txt state=absent'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h3 id="_5-file-模块"><a href="#_5-file-模块" class="header-anchor">#</a> 5）file 模块</h3> <p>该模块主要用于设置文件的属性，比如创建文件、创建链接文件、删除文件等。
下面是一些常见的命令：</p> <blockquote><p><code>force</code> # 需要在两种情况下强制创建软链接，一种是源文件不存在，但之后会建立的情况下；另一种是目标软链接已存在，需要先取消之前的软链，然后创建新的软链，有两个选项：yes|no
<code>group</code> # 定义文件/目录的属组。后面可以加上<code>mode</code>：定义文件/目录的权限
<code>owner</code> # 定义文件/目录的属主。后面必须跟上<code>path</code>：定义文件/目录的路径
<code>recurse</code> # 递归设置文件的属性，只对目录有效，后面跟上<code>src</code>：被链接的源文件路径，只应用于<code>state=link</code>的情况
<code>dest</code> # 被链接到的路径，只应用于<code>state=link</code>的情况
<code>state</code> # 状态，有以下选项：</p> <blockquote><p><code>directory</code>： # 如果目录不存在，就创建目录
<code>file</code>： # 即使文件不存在，也不会被创建
<code>link</code>： # 创建软链接
<code>hard</code>： # 创建硬链接
<code>touch</code>： # 如果文件不存在，则会创建一个新的文件，如果文件或目录已存在，则更新其最后修改时间
<code>absent</code>： # 删除目录、文件或者取消链接文件</p></blockquote></blockquote> <p>用法举例如下：</p> <h4 id="创建目录"><a href="#创建目录" class="header-anchor">#</a> 创建目录</h4> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>ansible <span class="token builtin class-name">test</span> -m <span class="token function">file</span> -a <span class="token string">'path=/tmp/tmp-dir state=directory'</span>
ansible <span class="token builtin class-name">test</span> -m shell -a <span class="token string">'ls -l /tmp | grep tmp-dir'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h4 id="创建空文件"><a href="#创建空文件" class="header-anchor">#</a> 创建空文件</h4> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>ansible <span class="token builtin class-name">test</span> -m <span class="token function">file</span> -a <span class="token string">'path=/tmp/tmp-dir/b.txt state=touch'</span>
ansible <span class="token builtin class-name">test</span> -m shell -a <span class="token string">'ls -l /tmp/tmp-dir/b.txt'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h4 id="创建链接文件"><a href="#创建链接文件" class="header-anchor">#</a> 创建链接文件</h4> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>ansible <span class="token builtin class-name">test</span> -m <span class="token function">file</span> -a <span class="token string">'path=/tmp/tmp-dir/a.txt src=/tmp/tmp-dir/b.txt state=link'</span>
ansible <span class="token builtin class-name">test</span> -m shell -a <span class="token string">'ls -l /tmp/tmp-dir/a.txt'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h4 id="删除文件"><a href="#删除文件" class="header-anchor">#</a> 删除文件</h4> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>ansible <span class="token builtin class-name">test</span> -m <span class="token function">file</span> -a <span class="token string">'path=/tmp/tmp-dir state=absent'</span>
ansible <span class="token builtin class-name">test</span> -m shell -a <span class="token string">'ls -l /tmp/tmp-dir'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="_6-fetch-模块"><a href="#_6-fetch-模块" class="header-anchor">#</a> 6）fetch 模块</h3> <p>该模块用于从远程某主机获取（复制）文件到本地。
有两个选项：</p> <blockquote><p><code>dest</code>：用来存放文件的目录
<code>src</code>：在远程拉取的文件，并且必须是一个<strong>file</strong>，不能是<strong>目录</strong></p></blockquote> <p>具体举例如下：</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>ansible <span class="token builtin class-name">test</span> -m fetch -a <span class="token string">'src=/etc/hostname dest=./'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="_7-cron-模块"><a href="#_7-cron-模块" class="header-anchor">#</a> 7）cron 模块</h3> <p>该模块适用于管理<code>cron</code>计划任务的。
其使用的语法跟我们的<code>crontab</code>文件中的语法一致，同时，可以指定以下选项：</p> <blockquote><p><code>day</code> # 日应该运行的工作( 1-31, *, */2, )
<code>hour</code> # 小时 ( 0-23, *, */2, )
<code>minute</code> #分钟( 0-59, *, */2, )
<code>month</code> # 月( 1-12, *, /2, )
<code>weekday</code> # 周 ( 0-6 for Sunday-Saturday,, )
<code>job</code> # 指明运行的命令是什么
<code>name</code> # 定时任务描述
<code>reboot</code> # 任务在重启时运行，不建议使用，建议使用special_time
<code>special_time</code> # 特殊的时间范围，参数：reboot（重启时），annually（每年），monthly（每月），weekly（每周），daily（每天），hourly（每小时）
<code>state</code> # 指定状态，present表示添加定时任务，也是默认设置，absent表示删除定时任务
<code>user</code> # 以哪个用户的身份执行</p></blockquote> <p>举例如下：</p> <h4 id="添加计划任务"><a href="#添加计划任务" class="header-anchor">#</a> 添加计划任务</h4> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>ansible <span class="token builtin class-name">test</span> -m <span class="token function">cron</span> -a <span class="token string">'name=&quot;ntp update every 5 min&quot; minute=*/5 job=&quot;/sbin/ntpdate 172.17.0.1 &amp;&gt; /dev/null&quot;'</span>
ansible <span class="token builtin class-name">test</span> -m shell -a <span class="token string">'crontab -l'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h4 id="删除计划任务"><a href="#删除计划任务" class="header-anchor">#</a> 删除计划任务</h4> <p>如果我们的计划任务添加错误，想要删除的话，则执行以下操作：
首先我们查看一下现有的计划任务：</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>ansible <span class="token builtin class-name">test</span> -m shell -a <span class="token string">'crontab -l'</span>
ansible <span class="token builtin class-name">test</span> -m <span class="token function">cron</span> -a <span class="token string">'name=&quot;df everyday&quot; hour=15 job=&quot;df -lh &gt;&gt; /tmp/disk_total &amp;&gt; /dev/null&quot; state=absent'</span>
ansible <span class="token builtin class-name">test</span> -m shell -a <span class="token string">'crontab -l'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="_8-yum-模块"><a href="#_8-yum-模块" class="header-anchor">#</a> 8）yum 模块</h3> <p>顾名思义，该模块主要用于软件的安装。
其选项如下：</p> <blockquote><p><code>name</code> # 所安装的包的名称
<code>state=</code> # <code>present</code>---&gt;安装， <code>latest</code>---&gt;安装最新的(升级), <code>absent</code>---&gt; 卸载软件。
<code>update_cache</code> # 强制更新yum的缓存
<code>conf_file</code> # 指定远程yum安装时所依赖的配置文件（安装本地已有的包）。
<code>disable_pgp_check</code> # 是否禁止GPG checking，只用于<code>present</code>or <code>latest</code>。
<code>disablerepo</code> # 临时禁止使用yum库。 只用于安装或更新时。
<code>enablerepo</code> # 临时使用的yum库。只用于安装或更新时。</p></blockquote> <p>下面我们就来安装一个包试试看：</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>ansible <span class="token builtin class-name">test</span> -m yum -a <span class="token string">'name=httpd state=present'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="_9-service-模块"><a href="#_9-service-模块" class="header-anchor">#</a> 9）service 模块</h3> <p>该模块用于服务程序的管理。
其主要选项如下：</p> <blockquote><p><code>arguments</code> # 命令行提供额外的参数
<code>enabled</code> # 设置开机启动。
<code>name</code> #服务名称
<code>runlevel</code> # 开机启动的级别，一般不用指定。
<code>sleep</code> # 在重启服务的过程中，是否等待。如在服务关闭以后等待2秒再启动。(定义在剧本中。)
<code>state</code> # 有四种状态，分别为：<code>started</code>---&gt;启动服务， <code>stopped</code>---&gt;停止服务， <code>restarted</code>---&gt;重启服务， <code>reloaded</code>---&gt;重载配置</p></blockquote> <p>下面是一些例子：</p> <h4 id="开启服务并设置自启动"><a href="#开启服务并设置自启动" class="header-anchor">#</a> 开启服务并设置自启动</h4> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>ansible <span class="token builtin class-name">test</span> -m <span class="token function">service</span> -a <span class="token string">'name=nginx state=started enabled=true'</span>
<span class="token comment"># 测试端口</span>
ansible <span class="token builtin class-name">test</span> -m shell -a <span class="token string">'ss -ntl'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h4 id="关闭服务"><a href="#关闭服务" class="header-anchor">#</a> 关闭服务</h4> <p>我们也可以通过该模块来关闭我们的服务：</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>ansible <span class="token builtin class-name">test</span> -m <span class="token function">service</span> -a <span class="token string">'name=nginx state=stopped'</span>
<span class="token comment"># 测试端口</span>
ansible <span class="token builtin class-name">test</span> -m shell -a <span class="token string">'ss -ntl'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="_10-user-模块"><a href="#_10-user-模块" class="header-anchor">#</a> 10）user 模块</h3> <p>该模块主要是用来管理用户账号。
其主要选项如下：</p> <blockquote><p><code>comment</code> # 用户的描述信息
<code>createhome</code> # 是否创建家目录
<code>force</code> # 在使用state=absent时, 行为与userdel –force一致.
<code>group</code> # 指定基本组
<code>groups</code> # 指定附加组，如果指定为(groups=)表示删除所有组
<code>home</code> # 指定用户家目录
<code>move_home</code> # 如果设置为home=时, 试图将用户主目录移动到指定的目录
<code>name</code> # 指定用户名
<code>non_unique</code> # 该选项允许改变非唯一的用户ID值
<code>password</code> # 指定用户密码
<code>remove</code> # 在使用state=absent时, 行为是与userdel –remove一致
<code>shell</code> # 指定默认shell
<code>state</code> # 设置帐号状态，不指定为创建，指定值为absent表示删除
<code>system</code> # 当创建一个用户，设置这个用户是系统用户。这个设置不能更改现有用户
<code>uid</code> # 指定用户的uid</p></blockquote> <p>举例如下：</p> <h4 id="添加一个用户并指定其-uid"><a href="#添加一个用户并指定其-uid" class="header-anchor">#</a> 添加一个用户并指定其 uid</h4> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>ansible <span class="token builtin class-name">test</span> -m user -a <span class="token string">'name=tuser1 uid=10001'</span>
ansible <span class="token builtin class-name">test</span> -m shell -a <span class="token string">'cat /etc/passwd | grep tuser1'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h4 id="添加一个用户并指定其-密码"><a href="#添加一个用户并指定其-密码" class="header-anchor">#</a> 添加一个用户并指定其 密码</h4> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>ansible <span class="token builtin class-name">test</span> -m user -a <span class="token string">&quot;name=tuser2 password={{'123456' | password_hash('sha512')}}&quot;</span>
ansible <span class="token builtin class-name">test</span> -m shell -a <span class="token string">'cat /etc/passwd | grep tuser2'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h4 id="删除用户"><a href="#删除用户" class="header-anchor">#</a> 删除用户</h4> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token comment"># remove 控制是否删除家目录 邮箱</span>
ansible <span class="token builtin class-name">test</span> -m user -a <span class="token string">'name=tuser1 state=absent remove=true'</span>
ansible <span class="token builtin class-name">test</span> -m shell -a <span class="token string">'cat /etc/passwd | grep tuser1'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="_11-group-模块"><a href="#_11-group-模块" class="header-anchor">#</a> 11）group 模块</h3> <p>该模块主要用于添加或删除组。
常用的选项如下：</p> <blockquote><p><code>gid</code> # 设置组的GID号
<code>name</code> # 指定组的名称
<code>state</code> # 指定组的状态，默认为创建，设置值为<code>absent</code>为删除
<code>system</code> # 设置值为<code>yes</code>，表示创建为系统组</p></blockquote> <p>举例如下：</p> <h4 id="创建组"><a href="#创建组" class="header-anchor">#</a> 创建组</h4> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>ansible <span class="token builtin class-name">test</span> -m group -a <span class="token string">'name=sanguo gid=12222'</span>
ansible <span class="token builtin class-name">test</span> -m shell -a <span class="token string">'cat /etc/group | grep 12222'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h4 id="删除组"><a href="#删除组" class="header-anchor">#</a> 删除组</h4> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>ansible <span class="token builtin class-name">test</span> -m group -a <span class="token string">'name=sanguo state=absent'</span>
ansible <span class="token builtin class-name">test</span> -m shell -a <span class="token string">'cat /etc/group | grep 12222'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="_12-script-模块"><a href="#_12-script-模块" class="header-anchor">#</a> 12）script 模块</h3> <p>该模块用于将本机的脚本在被管理端的机器上运行。
该模块直接指定脚本的路径即可，我们通过例子来看一看到底如何使用的：
首先，我们写一个脚本，并给其加上执行权限：</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token function">vim</span> ./test.sh	

<span class="token comment"># 添加如下内容</span>
<span class="token comment">#!/bin/bash	</span>
<span class="token function">date</span> <span class="token operator">&gt;&gt;</span> /tmp/current_date.log
<span class="token function">df</span> -lh <span class="token operator">&gt;&gt;</span> /tmp/disk_total.log
<span class="token function">cat</span> /tmp/current_date.log
<span class="token function">cat</span> /tmp/disk_total.log
<span class="token function">rm</span> -rf /tmp/current_date.log /tmp/disk_total.log
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>然后，我们直接运行命令来实现在被管理端执行该脚本：</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>ansible <span class="token builtin class-name">test</span> -m script -a <span class="token string">'./test.sh'</span>

CentOS7-Node1 <span class="token operator">|</span> CHANGED <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
    <span class="token string">&quot;changed&quot;</span><span class="token builtin class-name">:</span> true, 
    <span class="token string">&quot;rc&quot;</span><span class="token builtin class-name">:</span> <span class="token number">0</span>, 
    <span class="token string">&quot;stderr&quot;</span><span class="token builtin class-name">:</span> <span class="token string">&quot;Shared connection to centos7-node1 closed.<span class="token entity" title="\r">\r</span><span class="token entity" title="\n">\n</span>&quot;</span>, 
    <span class="token string">&quot;stderr_lines&quot;</span><span class="token builtin class-name">:</span> <span class="token punctuation">[</span>
        <span class="token string">&quot;Shared connection to centos7-node1 closed.&quot;</span>
    <span class="token punctuation">]</span>, 
    <span class="token string">&quot;stdout&quot;</span><span class="token builtin class-name">:</span> <span class="token string">&quot;2021年 10月 17日 星期日 22:29:33 CST<span class="token entity" title="\r">\r</span><span class="token entity" title="\n">\n</span>文件系统                 容量  已用  可用 已用% 挂载点<span class="token entity" title="\r">\r</span><span class="token entity" title="\n">\n</span>devtmpfs                 475M     0  475M    0% /dev<span class="token entity" title="\r">\r</span><span class="token entity" title="\n">\n</span>tmpfs                    487M     0  487M    0% /dev/shm<span class="token entity" title="\r">\r</span><span class="token entity" title="\n">\n</span>tmpfs                    487M  7.7M  479M    2% /run<span class="token entity" title="\r">\r</span><span class="token entity" title="\n">\n</span>tmpfs                    487M     0  487M    0% /sys/fs/cgroup<span class="token entity" title="\r">\r</span><span class="token entity" title="\n">\n</span>/dev/mapper/centos-root   17G  1.4G   16G    9% /<span class="token entity" title="\r">\r</span><span class="token entity" title="\n">\n</span>/dev/sda1               1014M  138M  877M   14% /boot<span class="token entity" title="\r">\r</span><span class="token entity" title="\n">\n</span>tmpfs                     98M     0   98M    0% /run/user/0<span class="token entity" title="\r">\r</span><span class="token entity" title="\n">\n</span>&quot;</span>, 
    <span class="token string">&quot;stdout_lines&quot;</span><span class="token builtin class-name">:</span> <span class="token punctuation">[</span>
        <span class="token string">&quot;2021年 10月 17日 星期日 22:29:33 CST&quot;</span>, 
        <span class="token string">&quot;文件系统                 容量  已用  可用 已用% 挂载点&quot;</span>, 
        <span class="token string">&quot;devtmpfs                 475M     0  475M    0% /dev&quot;</span>, 
        <span class="token string">&quot;tmpfs                    487M     0  487M    0% /dev/shm&quot;</span>, 
        <span class="token string">&quot;tmpfs                    487M  7.7M  479M    2% /run&quot;</span>, 
        <span class="token string">&quot;tmpfs                    487M     0  487M    0% /sys/fs/cgroup&quot;</span>, 
        <span class="token string">&quot;/dev/mapper/centos-root   17G  1.4G   16G    9% /&quot;</span>, 
        <span class="token string">&quot;/dev/sda1               1014M  138M  877M   14% /boot&quot;</span>, 
        <span class="token string">&quot;tmpfs                     98M     0   98M    0% /run/user/0&quot;</span>
    <span class="token punctuation">]</span>
<span class="token punctuation">}</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br></div></div><p>可以看出已经执行成功了。</p> <h3 id="_13-setup-模块"><a href="#_13-setup-模块" class="header-anchor">#</a> 13）setup 模块</h3> <p>该模块主要用于收集信息，是通过调用facts组件来实现的。
facts组件是Ansible用于采集被管机器设备信息的一个功能，我们可以使用setup模块查机器的所有facts信息，可以使用filter来查看指定信息。整个facts信息被包装在一个JSON格式的数据结构中，ansible_facts是最上层的值。
facts就是变量，内建变量 。每个主机的各种信息，cpu颗数、内存大小等。会存在facts中的某个变量中。调用后返回很多对应主机的信息，在后面的操作中可以根据不同的信息来做不同的操作。如redhat系列用yum安装，而debian系列用apt来安装软件。</p> <h4 id="查看信息"><a href="#查看信息" class="header-anchor">#</a> 查看信息</h4> <p>我们可以直接用命令获取到变量的值，具体我们来看看例子：</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token comment"># 查看所有信息</span>
ansible <span class="token builtin class-name">test</span> -m setup
<span class="token comment"># 过滤 filter 支持*通配符</span>
ansible <span class="token builtin class-name">test</span> -m setup -a <span class="token string">'filter=&quot;*mem*&quot;'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h4 id="保存信息"><a href="#保存信息" class="header-anchor">#</a> 保存信息</h4> <p>我们的setup模块还有一个很好用的功能就是可以保存我们所筛选的信息至我们的主机上，同时，文件名为我们被管制的主机的IP，这样方便我们知道是哪台机器出的问题。
我们可以看一看例子：</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>ansible <span class="token builtin class-name">test</span> -m setup -a <span class="token string">'filter=&quot;*mem*&quot;'</span> --tree /tmp/facts
<span class="token comment"># 然后我们可以去查看一下：</span>
<span class="token builtin class-name">cd</span> /tmp/facts/
<span class="token function">ls</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h3 id="其他模块"><a href="#其他模块" class="header-anchor">#</a> 其他模块</h3> <p>lineinfile
replace
yum_repository
lvg(依赖 yum lvm2)
lvol
parted
filesystem</p></div></section> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">Last Updated: </span> <span class="time">1/28/2022, 8:09:41 AM</span></div></footer> <!----> <div class="comments-wrapper"><!----></div> <ul class="side-bar sub-sidebar-wrapper" style="width:12rem;" data-v-70334359><li class="level-2" data-v-70334359><a href="/blogs/ansible/ansible-module.html#ansible-常用模块" class="sidebar-link reco-side-ansible-常用模块" data-v-70334359>ansible 常用模块</a></li><li class="level-3" data-v-70334359><a href="/blogs/ansible/ansible-module.html#_1-主机连通性测试" class="sidebar-link reco-side-_1-主机连通性测试" data-v-70334359>1）主机连通性测试</a></li><li class="level-3" data-v-70334359><a href="/blogs/ansible/ansible-module.html#_2-command-模块" class="sidebar-link reco-side-_2-command-模块" data-v-70334359>2）command 模块</a></li><li class="level-3" data-v-70334359><a href="/blogs/ansible/ansible-module.html#_3-shell-模块" class="sidebar-link reco-side-_3-shell-模块" data-v-70334359>3）shell 模块</a></li><li class="level-3" data-v-70334359><a href="/blogs/ansible/ansible-module.html#_4-copy-模块" class="sidebar-link reco-side-_4-copy-模块" data-v-70334359>4）copy 模块</a></li><li class="level-3" data-v-70334359><a href="/blogs/ansible/ansible-module.html#_5-file-模块" class="sidebar-link reco-side-_5-file-模块" data-v-70334359>5）file 模块</a></li><li class="level-3" data-v-70334359><a href="/blogs/ansible/ansible-module.html#_6-fetch-模块" class="sidebar-link reco-side-_6-fetch-模块" data-v-70334359>6）fetch 模块</a></li><li class="level-3" data-v-70334359><a href="/blogs/ansible/ansible-module.html#_7-cron-模块" class="sidebar-link reco-side-_7-cron-模块" data-v-70334359>7）cron 模块</a></li><li class="level-3" data-v-70334359><a href="/blogs/ansible/ansible-module.html#_8-yum-模块" class="sidebar-link reco-side-_8-yum-模块" data-v-70334359>8）yum 模块</a></li><li class="level-3" data-v-70334359><a href="/blogs/ansible/ansible-module.html#_9-service-模块" class="sidebar-link reco-side-_9-service-模块" data-v-70334359>9）service 模块</a></li><li class="level-3" data-v-70334359><a href="/blogs/ansible/ansible-module.html#_10-user-模块" class="sidebar-link reco-side-_10-user-模块" data-v-70334359>10）user 模块</a></li><li class="level-3" data-v-70334359><a href="/blogs/ansible/ansible-module.html#_11-group-模块" class="sidebar-link reco-side-_11-group-模块" data-v-70334359>11）group 模块</a></li><li class="level-3" data-v-70334359><a href="/blogs/ansible/ansible-module.html#_12-script-模块" class="sidebar-link reco-side-_12-script-模块" data-v-70334359>12）script 模块</a></li><li class="level-3" data-v-70334359><a href="/blogs/ansible/ansible-module.html#_13-setup-模块" class="sidebar-link reco-side-_13-setup-模块" data-v-70334359>13）setup 模块</a></li><li class="level-3" data-v-70334359><a href="/blogs/ansible/ansible-module.html#其他模块" class="sidebar-link reco-side-其他模块" data-v-70334359>其他模块</a></li></ul></main> <!----></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-c6073ba8 data-v-c6073ba8><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-c6073ba8><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-c6073ba8></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-c6073ba8></path></svg></div><div class="reco-bgm-panel" data-v-b1d3339e><audio id="bgm" src="/music/1.m4a" data-v-b1d3339e></audio> <div class="reco-float-box" style="bottom:200px;z-index:999999;display:none;" data-v-b1d3339e data-v-41bcba48 data-v-b1d3339e><img src="https://y.qq.com/music/photo_new/T002R300x300M0000046Etze42qCxC_1.jpg" data-v-b1d3339e></div> <div class="reco-bgm-box" style="left:10px;bottom:10px;z-index:999999;" data-v-b1d3339e data-v-41bcba48 data-v-b1d3339e><div class="reco-bgm-cover" style="background-image:url(https://y.qq.com/music/photo_new/T002R300x300M0000046Etze42qCxC_1.jpg);" data-v-b1d3339e><div class="mini-operation" style="display:none;" data-v-b1d3339e><i class="reco-bgm reco-bgm-pause" style="display:none;" data-v-b1d3339e></i> <i class="reco-bgm reco-bgm-play" style="display:none;" data-v-b1d3339e></i></div> <div class="falut-message" style="display:none;" data-v-b1d3339e>
          播放失败
        </div></div> <div class="reco-bgm-info" data-v-b1d3339e data-v-41bcba48 data-v-b1d3339e><div class="info-box" data-v-b1d3339e><i class="reco-bgm reco-bgm-music music" data-v-b1d3339e></i>苍穹</div> <div class="info-box" data-v-b1d3339e><i class="reco-bgm reco-bgm-artist" data-v-b1d3339e></i>韩磊</div> <div class="reco-bgm-progress" data-v-b1d3339e><div class="progress-bar" data-v-b1d3339e><div class="bar" data-v-b1d3339e></div></div></div> <div class="reco-bgm-operation" data-v-b1d3339e><i class="reco-bgm reco-bgm-last last" data-v-b1d3339e></i> <i class="reco-bgm reco-bgm-pause pause" style="display:none;" data-v-b1d3339e></i> <i class="reco-bgm reco-bgm-play play" data-v-b1d3339e></i> <i class="reco-bgm reco-bgm-next next" data-v-b1d3339e></i> <i class="reco-bgm reco-bgm-volume1 volume" data-v-b1d3339e></i> <i class="reco-bgm reco-bgm-mute mute" style="display:none;" data-v-b1d3339e></i> <div class="volume-bar" data-v-b1d3339e><div class="bar" data-v-b1d3339e></div></div></div></div> <div class="reco-bgm-left-box" data-v-b1d3339e data-v-41bcba48 data-v-b1d3339e><i class="reco-bgm reco-bgm-left" data-v-b1d3339e></i></div></div></div><!----></div></div>
    <script src="/assets/js/app.b9ccfc55.js" defer></script><script src="/assets/js/4.8390fbcb.js" defer></script><script src="/assets/js/1.3334f995.js" defer></script><script src="/assets/js/62.a1a460c7.js" defer></script>
  </body>
</html>
